import zipfile
import tkinter as tk
from tkinter import filedialog
from pathlib import Path

window = tk.Tk()
window.title('文件对话框')        # 设置窗口的标题
window.geometry('800x100')          # 设置窗口的大小

# 输入框
path_var = tk.StringVar()
entry = tk.Entry(window, textvariable=path_var, width=95)
entry.place(x=10, y=10, anchor='nw')

# 压缩明细
compress_var = tk.StringVar()
label = tk.Label(window, textvariable=compress_var, width=100, height=2, anchor='nw')
label.place(x=10, y=30)               # 将标签固定在窗口上

tk.Button(window, text='选择').place(x=700, y=10, anchor='nw')
tk.Button(window, text='压缩').place(x=750, y=10, anchor='nw')

window.mainloop()

def click():
    file_name= filedialog.askdirectory(title='选择一个文件夹',
                                  initialdir='./'           # 打开当前程序工作目录
                                             )
    path_var.set(file_name)

tk.Button(window, text='选择', command=click).place(x=700, y=10, anchor='nw')


def compress():
    select_path = path_var.get()        # 获取路径
    select_path = Path(select_path)
    zip_name = select_path.name + ".zip"        # 最终的压缩文件路径
    zip_path = select_path.parent.joinpath(zip_name)

    with zipfile.ZipFile(str(zip_path), 'w')as f:       # 进行压缩
        files = select_path.glob("**/*")        # 遍历被压缩文件的所有目录和文件
        for filename in files:
            compress_path = filename.relative_to(select_path)       # 这里要获得被压缩文件的相对路径
            compress_var.set(str(filename))                         # 显示被压缩文件
            label.update()                                          # 更新label
            f.write(str(filename), arcname=compress_path)           # 压缩写入

    compress_var.set('完成压缩')
    label.update()

tk.Button(window, text='压缩', command=compress).place(x=750, y=10, anchor='nw')